package com.xinou.daily_report.controller;

import com.alibaba.excel.EasyExcel;
import com.xinou.common.base.config.ApiResponse;
import com.xinou.common.base.exception.BusException;
import com.xinou.common.enumerate.HttpStatusEnum;
import com.xinou.daily_report.PO.WeekPlanPO;
import com.xinou.daily_report.PO.WeekResultPO;
import com.xinou.daily_report.PO.WeekResultUpdatePO;
import com.xinou.daily_report.VO.WeekPlanExcelVO;
import com.xinou.daily_report.VO.WeekResultExcelVO;
import com.xinou.daily_report.VO.WeekResultInfoVO;
import com.xinou.daily_report.mapper.SysWeekResultMapper;
import com.xinou.daily_report.service.SysWeekResultService;
import com.xinou.service.file.service.FileUploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-06-11
 */
@RestController
@RequestMapping("/admin/weekResult")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(tags = {"后台周结果"})
public class SysWeekResultController {
    private final SysWeekResultService sysWeekResultService;

    private final SysWeekResultMapper sysWeekResultMapper;

    private final FileUploadService fileUploadService;

    public static final String UploadFileUrl="https://image.xiaokesi.com/";

    @PostMapping("/list")
    @ApiOperation("获取周报结果列表")
    @RequiresPermissions({"admin:weekResult:list"})
    public ApiResponse<WeekResultInfoVO> weekResultList(@RequestBody WeekResultPO weekResultPO){
                System.out.println(weekResultPO);

        return sysWeekResultService.weekResultList(weekResultPO);
    }

    @PostMapping("/down")
    @ApiOperation("下载周结果")
    @RequiresPermissions({"admin:weekResult:down"})
    public ApiResponse<String> weekResultDown(@RequestBody WeekResultPO po) throws IOException {
        List<WeekResultExcelVO> vos = sysWeekResultMapper.weekResultDown(po);
        File tempFile = File.createTempFile(String.valueOf(System.nanoTime()),".xlsx");
        EasyExcel.write(tempFile, WeekResultExcelVO.class).sheet("周结果").doWrite(vos);
        String s = fileUploadService.fileUpload(tempFile).orElseThrow(() -> new BusException(HttpStatusEnum.RE_CODE_PARAM_ERROR, "下载周结果失败"));
        return new ApiResponse<>(UploadFileUrl + s);
    }

    @PostMapping("/save")
    @ApiOperation("添加周报结果")
    @RequiresPermissions({"admin:weekResult:save"})
    public ApiResponse<Void> weekResultSave(@RequestBody WeekResultUpdatePO weekResultUpdatePO){
        System.out.println(weekResultUpdatePO);
        return sysWeekResultService.weekResultSave(weekResultUpdatePO);
    }

    @PostMapping("/update")
    @ApiOperation("修改周报结果")
    @RequiresPermissions({"admin:weekResult:update"})
    public ApiResponse<Void> weekResultUpdate(@RequestBody WeekResultUpdatePO weekResultUpdatePO){
        return sysWeekResultService.weekResultUpdate(weekResultUpdatePO);
    }


    @PostMapping("/delete")
    @ApiOperation("删除周报结果")
    @RequiresPermissions({"admin:weekResult:delete"})
    public ApiResponse<Void> weekResultDelete(@RequestBody WeekResultPO weekResultPO){
        return sysWeekResultService.weekResultDelete(weekResultPO);
    }

    @PostMapping("/getById")
    @ApiOperation("根据id查询周报结果")
    @RequiresPermissions({"admin:weekResult:getById"})
    public ApiResponse<WeekResultInfoVO> weekResultGetById(@RequestBody WeekResultPO weekResultPO){
        return sysWeekResultService.weekResultGetById(weekResultPO);
    }


    @PostMapping("/getByIdNoPaging")
    @ApiOperation("根据id查询周报结果不分页")
    @RequiresPermissions({"admin:weekResult:getByIdNoPaging"})
    public ApiResponse<WeekResultInfoVO> weekResultGetByIdNoPaging(@RequestBody WeekResultPO weekResultPO){
        return sysWeekResultService.weekResultGetByIdNoPaging(weekResultPO);
    }


}

